package com.ruoyi.workbench.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.workbench.domain.WbVisitLog;
import com.ruoyi.workbench.domain.WbWholeUnit;
import com.ruoyi.workbench.domain.WbWholeUnitVisitLogNew;
import com.ruoyi.workbench.domain.WbWholeVisitLog;
import com.ruoyi.workbench.mapper.WbVisitLogMapper;
import com.ruoyi.workbench.service.IWbLeafUnitService;
import com.ruoyi.workbench.service.IWbVisitLogService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 拜访日志Service业务层处理
 *
 * @author hanjiafeng
 * @date 2021-02-25
 */
@Service
public class WbVisitLogServiceImpl implements IWbVisitLogService {
    private final WbVisitLogMapper wbVisitLogMapper;
    private final IWbLeafUnitService wbLeafUnitService;

    public WbVisitLogServiceImpl(WbVisitLogMapper wbVisitLogMapper, IWbLeafUnitService wbLeafUnitService) {
        this.wbVisitLogMapper = wbVisitLogMapper;
        this.wbLeafUnitService = wbLeafUnitService;
    }

    /**
     * 查询拜访日志
     *
     * @param id 拜访日志ID
     * @return 信息上报
     */
    @Override
    public WbVisitLog selectWbVisitLogById(Long id) {
        return wbVisitLogMapper.selectWbVisitLogById(id);
    }

    @Override
    public WbWholeVisitLog selectWbWholeVisitLogById(Long id) {
        WbVisitLog wbVisitLog = wbVisitLogMapper.selectWbVisitLogById(id);
        WbWholeUnit tempWholeUnit = wbLeafUnitService.selectWbWholeUnitById(wbVisitLog.getUnit7Id());
        WbWholeVisitLog tempWholeVisitLog = new WbWholeVisitLog();
        tempWholeVisitLog.setWholeUnit(tempWholeUnit);
        tempWholeVisitLog.setWbVisitLog(wbVisitLog);
        return tempWholeVisitLog;
    }

    /**
     * 查询拜访日志列表
     *
     * @param wbVisitLog 信息上报
     * @return 信息上报
     */
    @Override
    public List<WbVisitLog> selectWbVisitLogList(WbVisitLog wbVisitLog) {
        return wbVisitLogMapper.selectWbVisitLogList(wbVisitLog);
    }

    @Override
    public List<WbWholeVisitLog> selectWbWholeVisitLogList(WbWholeVisitLog wbWholeVisitLog) {

        return wbVisitLogMapper.selectWbWholeVisitLogList(wbWholeVisitLog);
    }

    /**
     * 新增拜访日志
     *
     * @param wbVisitLog 信息上报
     * @return 结果
     */
    @Override
    public int insertWbVisitLog(WbVisitLog wbVisitLog) {
        wbVisitLog.setCreateTime(DateUtils.getNowDate());
        wbVisitLog.setCreateBy(ShiroUtils.getLoginName());
        return wbVisitLogMapper.insertWbVisitLog(wbVisitLog);
    }

    /**
     * 修改拜访日志
     *
     * @param wbVisitLog 信息上报
     * @return 结果
     */
    @Override
    public int updateWbVisitLog(WbVisitLog wbVisitLog) {
        wbVisitLog.setUpdateTime(DateUtils.getNowDate());
        wbVisitLog.setUpdateBy(ShiroUtils.getLoginName());
        return wbVisitLogMapper.updateWbVisitLog(wbVisitLog);
    }

    /**
     * 删除拜访日志对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWbVisitLogByIds(String ids) {
        return wbVisitLogMapper.deleteWbVisitLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除拜访日志信息
     *
     * @param id 拜访日志ID
     * @return 结果
     */
    @Override
    public int deleteWbVisitLogById(Long id) {
        return wbVisitLogMapper.deleteWbVisitLogById(id);
    }

    @Override
    public List<WbWholeUnitVisitLogNew> selectWbWholeUnitVisitLogNewList(WbWholeUnitVisitLogNew wbWholeUnitVisitLogNew) {
        return wbVisitLogMapper.selectWbWholeUnitVisitLogNewList(wbWholeUnitVisitLogNew, null, null, null);
    }

    @Override
    public List<WbWholeUnitVisitLogNew> selectWbWholeUnitVisitLogNewListDuringTime(WbWholeUnitVisitLogNew wbWholeUnitVisitLogNew, Integer period, String startTime, String endTime, String... types) {
        return wbVisitLogMapper.selectWbWholeUnitVisitLogNewList(wbWholeUnitVisitLogNew, period, startTime, endTime, types);
    }

    @Override
    public String checkPhoneNumberUnique(Long id, String type, String phoneNumber) {
        int count = wbVisitLogMapper.checkPhoneNumberUnique(id, type, phoneNumber);
        if (count == 0) {
            return UserConstants.USER_PHONE_UNIQUE;
        } else {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
    }
}
